#pragma once
#include<cstdint>
#include <sys/epoll.h>
#include"Common.hpp"
#include"Log.hpp"
using namespace kzz;

//Epoll模型
class Epoller
{
    public:
    Epoller():_epfd(-1)
    {
        _epfd=epoll_create(128);
        if(_epfd<0)
        {
            LOG(LogLevel::ERROR)<<"epoll_create error";
            exit(EPOLL_ERR);
        }
        LOG(LogLevel::ERROR)<<"epoll_create success";

    }
    //等待
    int Wait(epoll_event* rvs,int rvs_num,int timeout)
    {
        //int n = epoll_wait(_epfd, _rvs, size, timeout);
        int n=epoll_wait(_epfd,rvs,rvs_num,timeout);
        if(n<0)
        {
            LOG(LogLevel::ERROR)<<"epoll_wait error";
            exit(EPOLL_ERR);
        }
        return n;
    }

    //添加事务·
    void AddEvent(int sockfd,uint32_t events)
    {
        epoll_event evs;
        evs.data.fd= sockfd;
        evs.events=EPOLLIN;
        int n=epoll_ctl(_epfd,EPOLL_CTL_ADD,sockfd,&evs);
        if(n<0)
        {
             LOG(LogLevel::ERROR)<<"epoll_create error";
            exit(EPOLL_ERR);
        }
         LOG(LogLevel::ERROR)<<"成功写入内核，fd"<<sockfd;

    }
    //删除事务
    void DelEvent()
    {

    }


    private:
    
    uint16_t _epfd;

    


};